<?php

class Setup_DomainController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
    }

    public function preDispatch()
    {
        parent::preDispatch();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        if(!$userId){
            return "";
        }
        
        $roleId = Ext_User::getInstance()->show($userId)->buildRole()->role->roleId;
        $acl = Ext_Acl::getInstance()->build($roleId);
        if(!$acl->isAllowed($roleId, 'setup', 'account')){
            $this->_forward("show-access-denied", "auth", "account");
        }
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();

        $domain    = $this->filter->filter($this->_request->getPost('domain'));
  
        $validater = new Ext_Validate();
        $validater->addValidater(new Add_Validate_Setup_Domain_SavePost_Domain($domain));
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }

        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        $domainOne = App_Model_DbTable_Domain::getInstance()->getRowByDomain($domain);
        if($domainOne){
            if($domainOne['userId'] < 1){
                $row = array (
                    'userId' => $userId,
                );
                App_Model_DbTable_Domain::getInstance()->addOld($row, $domain);
            }else{
                
            }
        }else{
            $row = array (
                'userId' => $userId,
                'domain' => $domain,
            );
            App_Model_DbTable_Domain::getInstance()->addNew($row);
        }
        
        $row = array (
            'domain' => $domain,
        );
        App_Model_DbTable_User::getInstance()->addOld($row, $userId);
        
        $viewResult->addMessages($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function showFormAction()
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();

        $viewForm = new Ext_View_Form_Setup_Profile();
        
        $userOne = App_Model_DbTable_User::getInstance()->getRowById($userId);
        $domain = $userOne['domain'];
        
        $this->view->domain = $domain;
        $this->view->preUrl = "http://".$_SERVER['HTTP_HOST'].$this->view->url(array('domain'=>''), "blogDomainFirst", true);
        //$this->view->preUrl = "http://".$_SERVER['HTTP_HOST'].$this->view->baseUrl("blog/");
    }
    
}

?>